package com.qf.service.impl;

import com.qf.mapper.ExamMapper;
import com.qf.pojo.Exam;
import com.qf.service.ExamService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    ExamMapper examMapper;
    @Override
    public ResultMsg list(Integer classId,Integer courseId,int page ,int limit) {
        if (page<1)
            return ResultMsg.FAILED(209,"页码有错");
        List<Exam> all = examMapper.findAll(classId,courseId,(page-1)*limit,limit);
        if (all.size()!=0)
            return ResultMsg.SUCCESS(0,all,all.size());
        return ResultMsg.FAILED(201,"查询失败");

    }

    @Override
    public ResultMsg deleteByid(List<Integer> examIds) {
        for (Integer examId : examIds) {
            int i = examMapper.deleteByid(examId);
            if (i==0)
                return ResultMsg.FAILED(206,"删除失败");
        }
       return ResultMsg.SUCCESS(null);
    }

    @Override
    public ResultMsg deleteByid(Integer examId) {
        int i = examMapper.deleteByid(examId);
        if (i==0)
            return ResultMsg.FAILED(204,"删除失败");
        return ResultMsg.SUCCESS(null);
    }

    @Override
    public ResultMsg selectByid(Integer examId) {
        Exam exam = examMapper.selectByid(examId);
        if (exam!=null)
            return ResultMsg.SUCCESS(exam);
        return ResultMsg.FAILED(210,"查询失败");
    }

    @Override
    public ResultMsg insert(Exam exam) {
        int insert = examMapper.insert(exam);
        if (insert==0)
            return ResultMsg.FAILED(204,"新增失败");
        return ResultMsg.SUCCESS(null);
    }

    @Override
    public ResultMsg updateByid(Exam exam) {
        int i = examMapper.updateByid(exam);
        if (i==0)
            return ResultMsg.FAILED(209,"修改失败");
        return ResultMsg.SUCCESS();
    }

    @Override
    public ResultMsg updatestatus(int id) {
        Exam exam = examMapper.selectByid(id);
        int examStatus = exam.getExamStatus();
        if (examStatus==1){
            int i = examMapper.updatestatus(id, 0);
            if (i==0)
                return ResultMsg.FAILED(208,"修改失败");
            return ResultMsg.SUCCESS();
        }else {
            int i = examMapper.updatestatus(id, 1);
            if (i==0)
                return ResultMsg.FAILED(208,"修改失败");
            return ResultMsg.SUCCESS();
        }
    }
}
